//
//  GuoJiaViewController.m
//  MDDNew
//
//  Created by 孙昊坤 on 15/10/28.
//  Copyright © 2015年 heqing. All rights reserved.
//

#pragma mark -- 国家界面（目的地的跳转界面）

// 宽高
#define Kheight [UIScreen mainScreen].bounds.size.height
#define Kwidth [UIScreen mainScreen].bounds.size.width

#import "GuoJiaViewController.h"

// 导入数据解析类
#import "TYDataManager.h"
// 导入Model类
#import "GuoJiaModel.h"

#import "UIImageView+WebCache.h"

#import "UIView+WLFrame.h"

// 轮播图
#import "SDCycleScrollView.h"

// 国家详细页面跳转
#import "GuoJiaShiYongXinXi.h"

// 导入cell
#import "ZYXCell.h"

// 导入自由行跳转页面
#import "ChaoJiZhiYouXing.h"
// 导入自由行查看全部跳转页面
#import "CJZYXCaKaQuanBu.h"
// 导入全部城市的跳转页面
#import "QuanBuChengShi.h"

// 导入城市详情跳转页面
#import "ChengShiXiangQing.h"

@interface GuoJiaViewController ()<UITableViewDataSource, UITableViewDelegate, /*轮播图协议*/SDCycleScrollViewDelegate /*表示图协议*/ ,UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>

// 创建数组保存数据
@property (nonatomic,strong) NSMutableArray *dataArray;

@property (nonatomic, strong) UITableView *tableView;
//
@property (nonatomic, strong) GuoJiaModel *model;
// 滚动视图图片数组
@property (nonatomic, strong) NSArray *photos;
// 表示图cell
@property (nonatomic, strong) UICollectionViewCell *biaoCell;
// 表示图
@property (nonatomic, strong) UICollectionView *collectionView;

@end

@implementation GuoJiaViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self jieXi];
    
    [self addUITableView];
}

// 解析数据
- (void)jieXi
{
    NSString *string = @"http://open.qyer.com/qyer/footprint/country_detail?client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=20&country_id=";
    NSString *string2 = [string stringByAppendingFormat:@"%@",self.ids];
    
    NSString *string3 = [string2 stringByAppendingString:@"&lat=40.02934527642203&lon=116.3373434040859&page=1&track_app_channel=App%2520Store&track_app_version=6.8&track_device_info=iPhone7%2C1&track_deviceid=3D292DEC-1F82-4FE7-9AE7-A1866F5F24E7&track_os=ios%25209.0.2&v=1"];
    
    [[TYDataManager shareManager] requestDataWithUrl:string3 block:^(NSData *data){
        
        NSMutableDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        
        NSDictionary *dictionAry = [dic valueForKey:@"data"];
        
        _model = [[GuoJiaModel alloc] init];
        
        [_model setValuesForKeysWithDictionary:dictionAry];
        
        
        _photos = _model.photos;
        [self.tableView reloadData];
        [self.collectionView reloadData];
    }];
}

- (void)addUITableView
{
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, -44, Kwidth, Kheight + 44) style:UITableViewStylePlain];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    [self.view addSubview:_tableView];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 1) {
        return 2;
    }
    if (section == 2) {
        // 获取自由行的数组个数
        NSArray *array = _model.zyx;
        if (array.count == 0) {
            return 0;
        } else {
            return array.count + 1;
        }
        
    }
    return 1;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    static NSString *string = @"MyCell";
    ZYXCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if (cell == nil) {
        cell = [[ZYXCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:string];
    }
    
    // 第一组滚动视图
    if (indexPath.section == 0) {
        // 照片数组
        NSArray *imagesURLStrings = _photos;
        
        //网络加载 --- 创建带标题的图片轮播器
        SDCycleScrollView *cycleScrollView2 = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, Kwidth, 250) imageURLStringsGroup:nil]; // 模拟网络延时情景
        cycleScrollView2.pageControlAliment = SDCycleScrollViewPageContolAlimentRight;
        cycleScrollView2.delegate = self;
        cycleScrollView2.dotColor = [UIColor yellowColor]; // 自定义分页控件小圆标颜色
        cycleScrollView2.placeholderImage = [UIImage imageNamed:@"placeholder"];
        // 隐藏圆表
        cycleScrollView2.showPageControl = NO;
        [cell addSubview:cycleScrollView2];
        
        //             --- 模拟加载延迟
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            cycleScrollView2.imageURLStringsGroup = imagesURLStrings;
        });
        
        // 图片滚动文字
        // 国家中文名
        UILabel *guoJia = [[UILabel alloc] initWithFrame:CGRectMake(50, cell.height + 50, Kwidth - 50 , 30)];
        guoJia.text = _model.cnname;
        guoJia.font = [UIFont systemFontOfSize:25];
        guoJia.textColor = [UIColor whiteColor];
//        guoJia.backgroundColor = [UIColor redColor];
        [cell addSubview:guoJia];
        
        // 国家英文名
        UILabel *yingWen = [[UILabel alloc] initWithFrame:CGRectMake(guoJia.left, guoJia.bottom, Kwidth - guoJia.left, 30)];
        yingWen.alpha = 0.6;
        yingWen.text = _model.enname;
        yingWen.textColor = [UIColor whiteColor];
        [cell addSubview:yingWen];
        
        // 国家简介
        UILabel *jianJie = [[UILabel alloc] initWithFrame:CGRectMake(yingWen.left, yingWen.bottom , Kwidth - yingWen.left * 2, 50)];
        jianJie.textColor = [UIColor whiteColor];
        jianJie.text = _model.entryCont;
        jianJie.numberOfLines = 0;
        jianJie.font = [UIFont systemFontOfSize:14];
        [cell addSubview:jianJie];
        
        // 国家实用信息跳转
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(Kwidth - 150, jianJie.bottom + 5, 130, 20);
//        button.backgroundColor = [UIColor redColor];
        button.titleLabel.font = [UIFont systemFontOfSize:12];
        [button setTitle:@"国家实用信息 >" forState:UIControlStateNormal];
        [button addTarget:self action:@selector(actionTiaoZhuan:) forControlEvents:UIControlEventTouchUpInside];
        [cell addSubview:button];
    }
    // 第二组
    if (indexPath.section == 1) {
        // **国城市
        if (indexPath.row == 0) {
            cell.textLabel.text = [NSString stringWithFormat:@"%@城市",_model.cnname];
            cell.textLabel.textColor = [UIColor grayColor];
            cell.textLabel.font = [UIFont systemFontOfSize:15];
            
            // 添加查看全部
            UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(Kwidth - 90, cell.textLabel.top - 3, 90, cell.height)];
            lable.text = @"查看全部 >";
            lable.font = [UIFont systemFontOfSize:13];
            lable.alpha = 0.6;
            [cell addSubview:lable];
        
        } else {
            // 城市图片
            UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
            layout.minimumLineSpacing = 0;
            layout.minimumInteritemSpacing = 0;
            layout.itemSize = CGSizeMake(Kwidth / 2, 120);
            layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
            _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, Kwidth, 240) collectionViewLayout:layout];
            _collectionView.dataSource = self;
            _collectionView.delegate = self;
            _collectionView.backgroundColor = [UIColor whiteColor];
            [cell addSubview:_collectionView];
        [_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"MyCell"];
        }
    }
    if (indexPath.section == 2) {
        if (indexPath.row == 0) {
            
            
            cell.textLabel. text = @"超级自由行";
            cell.textLabel.textColor = [UIColor grayColor];
            cell.textLabel.font = [UIFont systemFontOfSize:15];
            // 添加查看全部
            UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(Kwidth - 90, cell.textLabel.top - 3, 90, cell.height)];
            lable.text = @"查看全部 >";
            lable.font = [UIFont systemFontOfSize:13];
            lable.alpha = 0.6;
            [cell addSubview:lable];
            
        } else {
            NSString *string = [_model.zyx[indexPath.row - 1] valueForKey:@"title"];
            cell.biaoTi.text = string;
            [cell.tuPian sd_setImageWithURL:[_model.zyx[indexPath.row - 1] valueForKey:@"photo"]];
            cell.zheKou.text = [_model.zyx[indexPath.row - 1] valueForKey:@"priceoff"];
            // 加个
            NSMutableString *jiaGe = [NSMutableString stringWithString:[_model.zyx[indexPath.row - 1] valueForKey:@"price"]];
            NSRange range = NSMakeRange(0, 4);
            [jiaGe deleteCharactersInRange:range];
            
            NSString *str = [jiaGe stringByReplacingOccurrencesOfString:@"</em>" withString:@""];
            
            cell.jiaGe.text = str;
        }
        
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return 250;
    }
    if (indexPath.section == 1) {
        if (indexPath.row == 0) {
            return 40;
        } else {
            NSArray *array = _model.hot_city;
            if (array.count == 1 || array.count == 2) {
                _collectionView.frame = CGRectMake(0, 0, Kwidth, 150);
                return 150;
            } else {
                return 240;
            }
            
        }
    }
    if (indexPath.section == 2) {
        if (indexPath.row == 0) {
            return 40;
        }
        return 70;
    }
    return 250;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 跳转到全部城市
    if (indexPath.section == 1 && indexPath.row == 0) {
        QuanBuChengShi *viewVC = [[QuanBuChengShi alloc] init];
        viewVC.ids = _ids;
        [self.navigationController pushViewController:viewVC animated:YES];
    }
    
    // 跳转到全部自由行
    if (indexPath.section == 2 && indexPath.row == 0) {
        CJZYXCaKaQuanBu *viewVC = [[CJZYXCaKaQuanBu alloc] init];
        // 这里的ids是国家id
        viewVC.ids = self.ids;
        
        [self.navigationController pushViewController:viewVC animated:YES];
    }
    // 跳转到自由行web
    if (indexPath.section == 2 && indexPath.row != 0) {
        ChaoJiZhiYouXing *viewVC = [[ChaoJiZhiYouXing alloc] init];
        NSString *ids = [_model.zyx[indexPath.row - 1] valueForKey:@"id"];
        // 这里的id是自由行web的id不是国家id
        viewVC.ids = ids;
        [self.navigationController pushViewController:viewVC animated:YES];
    }

}


// 返回每个分区的Item数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    NSArray *array = _model.hot_city;
    
    return array.count;
}

// 返回每个item的方法
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    // 这个方法里面 包括了 咱们之前创建tableViewCell写的一堆
    // 必须有一步 必须要有 注册cell
    _biaoCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MyCell" forIndexPath:indexPath];
    
    // 系统没有像tableView一样 给咱们提供布局方式
    // 咱们要使用UICollectionViewCell 一般都是自定义再使用 跟tableView一样 所有的自定义控件都要加在contentView上面
//    _biaoCell.contentView.backgroundColor = [UIColor redColor];
    
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(5, 5, Kwidth / 2 - 10, 110)];
    imageView.backgroundColor = [UIColor blueColor];
    [_biaoCell addSubview:imageView];
    [imageView sd_setImageWithURL:[_model.hot_city[indexPath.row] valueForKey:@"photo"]];
    // 城市中文名
    UILabel *chengShi = [[UILabel alloc] initWithFrame:CGRectMake(0, imageView.height / 2 - 20, imageView.right - 3, 20)];
    chengShi.textAlignment = NSTextAlignmentCenter;
    chengShi.font = [UIFont systemFontOfSize:20];
    chengShi.textColor = [UIColor whiteColor];
//    chengShi.backgroundColor = [UIColor redColor];
    [imageView addSubview:chengShi];
    
    NSArray *array = _model.hot_city;
    chengShi.text = [array[indexPath.row] valueForKey:@"cnname"];
    // 城市英文名
    UILabel *chengShiYing = [[UILabel alloc] initWithFrame:CGRectMake(0, chengShi.bottom, chengShi.right, 20)];
    chengShiYing.textAlignment = NSTextAlignmentCenter;
//    chengShiYing.backgroundColor = [UIColor greenColor];
    chengShiYing.textColor = [UIColor whiteColor];
    chengShiYing.font = [UIFont systemFontOfSize:15];
    [imageView addSubview:chengShiYing];
    chengShiYing.text = [array[indexPath.row] valueForKey:@"enname"];
    
    
    return _biaoCell; // 不需要释放
}

// 跳转方法
- (void)actionTiaoZhuan:(UIButton *)button{
    GuoJiaShiYongXinXi *gjVC = [[GuoJiaShiYongXinXi alloc] init];
    gjVC.ids = _model.ids;
    [self.navigationController pushViewController:gjVC animated:YES];
}

// 表示图点击
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    // 城市详情页面跳转
    ChengShiXiangQing *viewVC = [[ChengShiXiangQing alloc] init];
    NSArray *array = _model.hot_city;
    viewVC.ids = [array[indexPath.row] valueForKey:@"id" ];
  //  NSLog(@"%@", viewVC.ids);
    [self.navigationController pushViewController:viewVC animated:YES];
}

@end
